
#include <windows.h>
#include "d3d_obj.h"
#include <list>
#include <string>
#include <xcalibur.h>

using namespace xct;

#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")



LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

XCT_D3DOBJ d3dobj;
PerformanceTimer timer;

void renderz(void);
void initspts(void);

float x, y;
float frametime, upd = 0.5f;
int bg, anim, bmp;
int font1, font2;

int currframe = 0;

RECT frame = {0, 0, 1024/5, 128};

RECT rect;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
	HWND hWnd;
	MSG msg = {NULL};
	WNDCLASSEX wndclass = {0};

	const LPCSTR WindowClass = "WinApp";
	LPCSTR WindowTitle = "XCALIBUR";

	wndclass.cbSize         = sizeof(wndclass);
	wndclass.style          = CS_HREDRAW | CS_VREDRAW;
	wndclass.lpszClassName  = WindowClass;
	wndclass.lpfnWndProc    = WndProc;
	wndclass.hInstance      = hInstance;
	wndclass.hbrBackground  = (HBRUSH) COLOR_BACKGROUND;
	wndclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
	wndclass.hIcon          = LoadIcon(NULL, IDI_APPLICATION);
	wndclass.hIconSm        = wndclass.hIcon;

	RegisterClassEx(&wndclass);

	hWnd = CreateWindow(
		wndclass.lpszClassName,
		WindowTitle,
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		SCREEN_WIDTH,
		SCREEN_HEIGHT,
		NULL,
		NULL,
		hInstance,
		NULL);

	if(!hWnd)
		return EXIT_FAILURE;


	ShowWindow(hWnd, nShowCmd);
	UpdateWindow(hWnd);


	d3dobj.InitXCT_D3D(hWnd);

	font1 = d3dobj.LoadFont(30, 0, FALSE, "Consolas");
	font2 = d3dobj.LoadFont(62, 0, FALSE, "Chiller");


	initspts();

	timer.Initialize();


	/* Game loop */
	while(msg.message != WM_QUIT)
	{
		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else
		{
			renderz();
		}
	}
	UnregisterClass(WindowClass, hInstance);
	return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{

	RECT client;
	GetClientRect(hWnd, &client);
	GetClientRect(hWnd, &rect);

	switch(nMsg)
	{
	case WM_DESTROY:
		{
			d3dobj.UnloadAllSprites();
			d3dobj.UnloadAllFonts();
			PostQuitMessage(EXIT_SUCCESS);
			break;
		}
	case WM_SIZE :
		{
			d3dobj.ResetVP();
			break;
		}
	case WM_KEYDOWN:
		if(wParam == VK_LEFT)
			x -= 10;
		if(wParam == VK_RIGHT)
			x += 10;
		if(wParam == VK_UP)
			y -= 10;
		if(wParam == VK_DOWN)
			y += 10;
		if(wParam == VK_NUMPAD1)
			upd += 0.05f;
		if(wParam == VK_NUMPAD2)
			upd -= 0.05f;
			

		if ( x <= 0 ) x += 10;
		if ( x+160 >= client.right ) x -= 10;
		if ( y <= 0 ) y += 10;
		if ( y+95 >= client.bottom ) y -= 10;

		if(wParam == 0x1b)
			exit(0);
		break;

	} /* <- End of switch(nMsg) */
	return DefWindowProc(hWnd, nMsg, wParam, lParam);
}


void initspts(void)
{
	bg = d3dobj.LoadSprite("BG.bmp", colorkey(255, 255, 0, 0));
	anim = d3dobj.LoadSprite("RyuAttack.bmp", colorkey(255, 0, 0, 0));
	bmp = d3dobj.LoadSprite("bmp.bmp", colorkey(255, 255, 255, 255));
}

void renderz(void)
{
	d3dobj.Begin();

   
	d3dobj.RenderSprite(bg, 0, 0, WHITE); /* background */
	
	if ( currframe >= 4)
		currframe = 0;

	frametime += timer.Update();
	
	if ( frametime >= upd )
	{
		++currframe;
		frametime = 0.0f;

		frame.left = (currframe * 1024/5);
		frame.right = (frame.left + 1024/5);
	}


	int dir = 1; // 1 left, 2 right, 3 up, 4 down
	
	
	d3dobj.RenderText(font1, "> xevion <\n", x, y - 80, colorkey(100, 0, 65, 255));
	d3dobj.RenderText(font1, "     soft", x, y - 30, colorkey(100, 0, 65, 255));

	d3dobj.RenderText(font1, "[!] Animated sprite [!]", x+(-50), y+150, colorkey(100, 170, 0, 0));

	string buff = "Don't move.";
	D3DCOLOR curcolor = D3DCOLOR_ARGB(255, 0, 0, 0);
	
	// the ugliest code i ever created:
	if ( GetAsyncKeyState(VK_LEFT) )               {
		buff = "Move left.";
		dir = 1;
		curcolor = D3DCOLOR_ARGB(255, 0, 255, 0);  }
	else if ( GetAsyncKeyState(VK_RIGHT) )         {
		buff = "Move right.";
		dir = 2;
		curcolor = D3DCOLOR_ARGB(255, 255, 0, 0);  }
	else if ( GetAsyncKeyState(VK_UP) )            {
		buff = "Move up.";
		dir = 3;
		curcolor = D3DCOLOR_ARGB(255, 100, 0, 255);  }
	else if ( GetAsyncKeyState(VK_DOWN) )          {
		buff = "Move down.";
		dir = 4;
		curcolor = D3DCOLOR_ARGB(255, 170, 170, 0);
	}

	d3dobj.RenderSprite(bmp, 10, 10, WHITE);
	d3dobj.RenderFrame(anim, x, y, frame, curcolor); /* img1 */	

	d3dobj.RenderText(font1, buff.c_str(), 10, 10, curcolor);

	d3dobj.End();
}

